﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using HYRMEOpening;

namespace YArchitech.Revit.MEP.Opening
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdCheckOpening : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			string pathName = cmdData.Application.ActiveUIDocument.Document.PathName;
			if (pathName == "")
			{
				MessageBox.Show("请先保存项目。");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Document document = cmdData.Application.ActiveUIDocument.Document;
			try
			{
				if (CmdCheckOpening._hWndRevit == null)
				{
					CmdCheckOpening._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				ConfigDataXMLSelectForm configDataXMLSelectForm = new ConfigDataXMLSelectForm(true);
				string str = XmlUtils.SplitPathName(pathName) + ".xml";
				configDataXMLSelectForm.FileName = Product.UserDataLocation + "\\" + str;
				if (configDataXMLSelectForm.ShowDialog() == DialogResult.OK)
				{
					bool server = false;
					string fileName = configDataXMLSelectForm.FileName;
					ShowDlgApp.ThisApp.ShowDlgCheckOpening(cmdData.Application, server, fileName);
				}
			}
			catch
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static WindowHandle _hWndRevit;
	}
}
